<script setup lang='ts'>
  import { onMounted, reactive, ref, nextTick } from 'vue';
  import { gamesApi } from "@/api/controller";
  import { ToastSuccess, ToastWarning, Toastloading } from '@/utils/public';
  import serachGift from '@/components/serachGift/index.vue';
  const $emit = defineEmits(['updateList'])
  const formLabelWidth = '140px'
  const dataForm = ref()
  const action = reactive({
    disabled:false,
    dialogFormVisible:false,
    title:'',
    showType:[] as Array<any>
  })
  interface formQuert {
    shop_id:number
    gift_id:number
    energy_coin:number | string
    if_show:number

  }
  let form = reactive({} as formQuert)
  const formRules = {
    gift_id:[{ required: true, trigger: 'blur', message: '请选择礼物' }],
    energy_coin	:[{ required: true, trigger: 'blur', message: '请输入能量币数量' }],
    if_show:[{ required: true, trigger: 'blur', message: '请选择是否显示' }]
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      let submitSet = false
      if(form.shop_id) {
        await gamesApi.angerbirdUpdateShop(form).then(res=>{
          ToastSuccess('修改成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }else {
        await gamesApi.angerbirdAddShop(form).then(res=>{
          ToastSuccess('新增成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }
      if(!submitSet) return
      loading.close()
      $emit("updateList");
      closeDialog()
    })
  }
  const openDialog = (query:any = {})=>{
    let { gift_id='', energy_coin='', if_show='', id:shop_id='', showType=[] } = query
    action.showType = showType
    const obj = {
      gift_id,
      energy_coin,
      if_show,
    } as any
    if(shop_id) obj['shop_id'] = shop_id
    action.title = shop_id?'编辑商品':'新增商品'
    Object.assign(form,obj)
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="45%"
      :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="礼物" prop="gift_id">
          <serachGift
            version="0.0.1"
            :editTheStatus="form.shop_id?false:true"
            v-model:gift_id="form.gift_id" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="能量币数量" prop="energy_coin">
          <el-input
            v-model="form.energy_coin"
            placeholder="请输入能量币数量"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="是否显示" prop="is_show">
          <el-select v-model="form.if_show" placeholder="请选择是否显示">
            <el-option v-for="item in action.showType" :label="item.label" :value="item.type" />
          </el-select>
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>